package com.tutk.sample.AVAPI;

import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;

public class AudioEncoder {
	//声道数
    private static final int KEY_CHANNEL_COUNT = 1;
    //采样率
    private static final int KEY_SAMPLE_RATE = 8000;
	//相对于上面的音频录制，我们需要一个编码器的实例
    private static int audioSource = MediaRecorder.AudioSource.MIC;
    // 设置音频的录制的声道CHANNEL_IN_STEREO为双声道，CHANNEL_CONFIGURATION_MONO为单声道  
    private static int channelConfig = AudioFormat.CHANNEL_IN_MONO;  
    // 音频数据格式:PCM 16位每个样本。保证设备支持。PCM 8位每个样本。不一定能得到设备支持。  
    private static int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
    private AudioRecord audioRecord;  
    public  boolean isRecord = false;// 设置正在录制的状态
	private int bufferSize;
    byte[] data;
    byte[] okbyte;



    public void prepare() {


		//音频录制实例化和录制过程中需要用到的数据
		bufferSize = AudioRecord.getMinBufferSize(KEY_SAMPLE_RATE, channelConfig, audioFormat);
        data=new byte[bufferSize];
        okbyte=new byte[bufferSize/2];
		//实例化AudioRecord
		audioRecord=new AudioRecord(audioSource,KEY_SAMPLE_RATE,channelConfig,
		    audioFormat,bufferSize*2);
		
		audioRecord.startRecording();
//        LogHelper.logE("shurun",audioRecord.getSampleRate()+"pinlv");
        isRecord=true;
	
    
    }
    public void stopRecord() {
		// TODO Auto-generated method stub
    	audioRecord.stop();
    	audioRecord.release();
		audioRecord=null;
        isRecord=false;
	}


    public byte[] testRecord(){

   	 if (isRecord){
         int length=audioRecord.read(data,0,640);
         int ret=G711.encode(data, 0, length, okbyte);
         return okbyte;
     }
   	return null;


    }



    
}
    
    
	


